package com.canxi.earn.controller;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.canxi.common.annotation.RepeatSubmit;
import com.canxi.common.annotation.Log;
import com.canxi.common.core.controller.BaseController;
import com.canxi.common.core.domain.PageQuery;
import com.canxi.common.core.domain.R;
import com.canxi.common.core.validate.AddGroup;
import com.canxi.common.core.validate.EditGroup;
import com.canxi.common.enums.BusinessType;
import com.canxi.common.utils.poi.ExcelUtil;
import com.canxi.earn.domain.vo.CxJokeCategoryVo;
import com.canxi.earn.domain.bo.CxJokeCategoryBo;
import com.canxi.earn.service.ICxJokeCategoryService;

/**
 * 笑话分类
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/earn/jokeCategory")
public class CxJokeCategoryController extends BaseController {

    private final ICxJokeCategoryService iCxJokeCategoryService;


/**
     * 查询笑话分类列表
     */
    @SaCheckPermission("earn:jokeCategory:list")
    @GetMapping("/list")
        public R<List<CxJokeCategoryVo>> list(CxJokeCategoryBo bo) {
            return R.ok("查询成功", iCxJokeCategoryService.queryCxJokeCategoryAll(bo));
        }

    /**
     * 获取树形结构
     * @param bo
     * @return
     */
    @SaCheckPermission("earn:jokeCategory:list")
    @GetMapping("/listTree")
    public R<List<Tree<String>>> listTree(CxJokeCategoryBo bo) {
        return R.ok("查询成功", iCxJokeCategoryService.queryCxJokeCategoryTree(bo));
    }

    /**
     * 导出笑话分类列表
     */
    @SaCheckPermission("earn:jokeCategory:export")
    @Log(title = "笑话分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CxJokeCategoryBo bo, HttpServletResponse response) {
        List<CxJokeCategoryVo> list = iCxJokeCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "笑话分类", CxJokeCategoryVo.class, response);
    }

    /**
     * 获取笑话分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("earn:jokeCategory:query")
    @GetMapping("/{id}")
    public R<CxJokeCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iCxJokeCategoryService.queryById(id));
    }

    /**
     * 新增笑话分类
     */
    @SaCheckPermission("earn:jokeCategory:add")
    @Log(title = "笑话分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CxJokeCategoryBo bo) {
        return toAjax(iCxJokeCategoryService.insertByBo(bo));
    }

    /**
     * 修改笑话分类
     */
    @SaCheckPermission("earn:jokeCategory:edit")
    @Log(title = "笑话分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CxJokeCategoryBo bo) {
        return toAjax(iCxJokeCategoryService.updateByBo(bo));
    }

    /**
     * 删除笑话分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("earn:jokeCategory:remove")
    @Log(title = "笑话分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iCxJokeCategoryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
